﻿// TcpipDlg.cpp: 实现文件
//

#include "pch.h"
#include "ProjectAA.h"
#include "TcpipDlg.h"
#include "afxdialogex.h"


// CTcpipDlg 对话框

IMPLEMENT_DYNAMIC(CTcpipDlg, CDialogEx)

CTcpipDlg::CTcpipDlg(CWnd* pParent /*=nullptr*/)
	: CDialogEx(IDD_DIALOG_TCPIP, pParent)
	, m_nPort(0)
	, m_bCheckAuto(FALSE)
	, glueWidthUp(0)
	, glueWidthLow(0)
{

}

CTcpipDlg::~CTcpipDlg()
{
}

void CTcpipDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialogEx::DoDataExchange(pDX);
	DDX_Control(pDX, IDC_COMBO_MODE, m_comboTcpMode);
	DDX_Control(pDX, IDC_IPADDRESS_IP, m_ipAdd);
	DDX_Text(pDX, IDC_EDIT_PORT, m_nPort);
	DDX_Check(pDX, IDC_CHECK_AUTO, m_bCheckAuto);
	DDX_Text(pDX, IDC_EDIT1, glueWidthUp);
	DDX_Text(pDX, IDC_EDIT2, glueWidthLow);
}


BEGIN_MESSAGE_MAP(CTcpipDlg, CDialogEx)
	ON_BN_CLICKED(IDC_BUTTON_SAVE, &CTcpipDlg::OnBnClickedButtonSave)
	ON_BN_CLICKED(IDC_BUTTON_BUILD, &CTcpipDlg::OnBnClickedButtonBuild)
	ON_BN_CLICKED(IDC_BUTTON1, &CTcpipDlg::OnBnClickedButton1)
END_MESSAGE_MAP()


// CTcpipDlg 消息处理程序


BOOL CTcpipDlg::OnInitDialog()
{
	CDialogEx::OnInitDialog();

	ReadConfigIni();

	m_comboTcpMode.ResetContent();
	m_comboTcpMode.AddString(L"0: 服务器");
	m_comboTcpMode.AddString(L"1: 客户端");
	m_comboTcpMode.SetCurSel(gSystemMgr.m_nModeSelect);

	return TRUE;  // return TRUE unless you set the focus to a control
	// 异常: OCX 属性页应返回 FALSE
}


void CTcpipDlg::OnCancel()
{
	// TODO: 在此添加专用代码和/或调用基类

	CDialogEx::OnCancel();
}


void CTcpipDlg::OnBnClickedButtonSave()
{
	SaveValueToConfig();
}


void CTcpipDlg::OnBnClickedButtonBuild()
{
	if (gSystemMgr.m_nModeSelect == 0) {
		USES_CONVERSION;
		char* c_strIp = T2A(gSystemMgr.m_cstrIPAdd);
		bool bCreateServer = gSystemMgr.m_cSocketServer.OpenSocketServer(const_cast<char*>("Server"), c_strIp, gSystemMgr.m_nPort);
		CString cstr;
		cstr.Format(L"Socket Server Build result:%d", bCreateServer);
		gSystemLog.DisplayLogAndSave(cstr.GetBuffer(), LV_ERROR);
		if (bCreateServer)
		{
			gSystemMgr.StartSocketAcceptClientThread();
		}
	}
}

void CTcpipDlg::ReadConfigIni()
{
	m_ipAdd.SetWindowText(gSystemMgr.m_cstrIPAdd);
	m_nPort = gSystemMgr.m_nPort;
	(gSystemMgr.m_bAutoFlag == 1) ? m_bCheckAuto = true : m_bCheckAuto = false;

	glueWidthUp = gSystemMgr.glueWidthUp;
	glueWidthLow = gSystemMgr.glueWidthLow;

	UpdateData(FALSE);
}

void CTcpipDlg::SaveValueToConfig()
{
	UpdateData(TRUE);

	m_ipAdd.GetWindowText(gSystemMgr.m_cstrIPAdd);
	gSystemMgr.m_nPort = m_nPort;
	(m_bCheckAuto == TRUE) ? gSystemMgr.m_bAutoFlag = 1 : gSystemMgr.m_bAutoFlag = 0;
	gSystemMgr.m_nModeSelect = m_comboTcpMode.GetCurSel();
	CString cstr;
	cstr.Format(L"%d", gSystemMgr.m_nModeSelect);
	gSystemMgr.m_cTcpipConfigIni.SaveConfigIni(L"TcpipConfig", L"m_nModeSelect", cstr);

	gSystemMgr.m_cTcpipConfigIni.SaveConfigIni(L"TcpipConfig", L"m_cstrIPAdd", gSystemMgr.m_cstrIPAdd);

	cstr.Format(L"%d", gSystemMgr.m_nPort);
	gSystemMgr.m_cTcpipConfigIni.SaveConfigIni(L"TcpipConfig", L"m_nPort", cstr);

	cstr.Format(L"%d", gSystemMgr.m_bAutoFlag);
	gSystemMgr.m_cTcpipConfigIni.SaveConfigIni(L"TcpipConfig", L"m_bAutoFlag", cstr);

	gSystemMgr.glueWidthUp = glueWidthUp;
	gSystemMgr.glueWidthLow = glueWidthLow;
	cstr.Format(L"%.4f", gSystemMgr.glueWidthUp);	gSystemMgr.m_cTcpipConfigIni.SaveConfigIni(L"TcpipConfig", L"glueWidthUp", cstr);
	cstr.Format(L"%.4f", gSystemMgr.glueWidthLow);	gSystemMgr.m_cTcpipConfigIni.SaveConfigIni(L"TcpipConfig", L"glueWidthLow", cstr);

	gSystemLog.DisplayLogAndSave(L"Tcpip保存数据完成", LV_EVENT);
}



void CTcpipDlg::OnBnClickedButton1()
{
	CString cstr;
	cstr.Format(L"GLUETEST_test");
	//m_cClientGlue.m_sendBuffer.push_front(L"GLUETEST");
	//gSystemMgr.m_cClientGlue.m_sendBuffer.push_front(cstr.GetBuffer());
	gSystemMgr.m_cClientGlue.m_sendBuffer.push_front(L"Trigger");
}
